WebClipTest = TestCase("WebClipTest");

var FakeOAuth = function() {
  this.url_ = '';
  this.callback_ = '';
  this.params_ = '';

  this.sendSignedRequest = function(url, callback, params) {
    this.url_ = url;
    this.callback_ = callback;
    this.params_ = params;
  }

  this.getCallBack = function() {
    return this.callback_;
  }

  this.getUrl = function() {
    return this.url_;
  }
  
  this.getParams = function() {
    return this.params_;
  } 
} 

var noopCallback = function() {} 

WebClipTest.prototype.testCreateEmptyDocument = function() {
  var oauth = new FakeOAuth();
  webclip.createEmptyDocument('foo', noopCallback, oauth);

  assertEquals(webclip.constants.FEEDS_FULL, oauth.getUrl());
//  assertEquals(noopCallback, oauth.getCallBack());
  assertTrue(oauth.getParams()['body'].indexOf('<title>foo</title>') >= 0);
}

WebClipTest.prototype.testFetchDocument = function() {
  var oauth = new FakeOAuth();
  webclip.fetchDocument('1234', noopCallback, oauth);

  assertEquals(webclip.constants.FEEDS_ROOT + '/download/documents/Export', oauth.getUrl());
  assertEquals(noopCallback, oauth.getCallBack());
  assertEquals('1234', oauth.getParams()['parameters']['docID']);
  assertEquals('html', oauth.getParams()['parameters']['format']);
}
  
WebClipTest.prototype.testDocIdIsFoundInDocument = function() {
  var response = 'something to parse <gd:resourceId>document:1234</gd:resourceId>' +
      'something else to parse';
  
  assertEquals('1234', webclip.getDocIdFromResponse(response));
}

WebClipTest.prototype.testDocIdIsNotFoundInDocument = function() {
  var response = 'something to parse';
  assertEquals(null, webclip.getDocIdFromResponse(response));
}

WebClipTest.prototype.testJsToGetPageSelectionIsValid = function() {
  var jsCode = webclip.createJsToGetPageSelection('foo', 'bar');
  assertContains('"title": "foo"', jsCode);
  assertContains('"url": "bar"', jsCode);
}

WebClipTest.prototype.testClipFormatIsCorrect = function() {
  var clip = webclip.createClip('mytitle', new Date(1000424220000),
      'http://foo', 'textselected');
  assertContains('<b>mytitle</b>', clip);
  assertContains('9/14/2001', clip);
  assertContains('textselected', clip);
  assertContains('http://foo', clip);
}

WebClipTest.prototype.testUpdateDocument = function() {
  var oauth = new FakeOAuth();
  webclip.updateDocument('newtext', '1234', noopCallback, oauth);

  assertEquals(noopCallback, oauth.getCallBack());
  assertContains('1234', oauth.getUrl());
  assertEquals('newtext', oauth.getParams()['body']);
}

var FakeEndToEndOAuth = function() {
  this.counter_ = 0;

  this.sendSignedRequest = function(url, callback, params) {
    this.counter_ += 1;
    if (this.counter_ == 1) {
      callback('<gd:resourceId>document:1234</gd:resourceId>');
    } else if (this.counter_ == 2) {
      callback('something written before');
    } else if (this.counter_ == 3) {
      callback('foo');
    } else {
      throw 'not expected';
    }
  }
}

WebClipTest.prototype.testSaveSelection = function() {
  var oauth = new FakeEndToEndOAuth();
  var request = {
    'title': 'my title',
    'url': 'http://foo',
    'selection': 'bar'
  };

  var selection = '';
  webclip.saveSelection(request, new Date(), oauth, function(title, text) {
    selection = text;
  });
  
  assertContains('bar', selection);
}

assertContains = function(expected, string) {
  if (string.indexOf(expected) < 0) {
    var error = sprintf('Cannot find \'%s\' in \'%s\'', expected, string); 
    throw error;
  }
}

